package com.dataseek.iot.sys.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dataseek.iot.toolikit.common.consts.SysConsts;
import com.dataseek.iot.sys.entity.SysUserTable;
import com.dataseek.iot.sys.service.DbService;
import com.dataseek.iot.sys.service.SysUserService;
import com.dataseek.iot.sys.vo.SysUserVo;
import com.dataseek.iot.toolikit.common.enums.RspStatusEnum;
import com.dataseek.iot.toolikit.common.exception.UnAuthorizedException;
import com.dataseek.iot.toolikit.common.response.ResponseResult;
import com.dataseek.iot.toolkit.auth.entity.LoginUser;
import com.dataseek.iot.toolkit.auth.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("**/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private DbService dbService;


    /**
     * 查询用户列表
     *
     * @param userName 用户名称
     * @param telephone 手机号码
     * @param status 用户状态
     * @return
     */

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ResponseResult queryUser(@RequestParam(value="page")  Integer page, @RequestParam(value="size")  Integer size,@RequestParam(value="userName", required=false) String userName,@RequestParam(value="telephone", required=false) String telephone, @RequestParam(value="status", required=false)  String status, @RequestParam(value="deptName", required=false)  String deptName, @RequestParam(value="roleName", required=false)  String roleName, @RequestParam(value="userId", required=false)  Integer userId) throws IOException {

        SysUserVo sysUserVo = sysUserService.queryUser(userName,telephone,deptName,roleName,userId,status,page,size);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功",sysUserVo);
    }

    /**
     * 修改用户列表
     *
     * @param sysUserTable 用户实体类
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult updateUser(@RequestBody SysUserTable sysUserTable) throws IOException {
        Integer code  = sysUserService.updateUser(sysUserTable);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }

    /**
     * 新建用户
     *
     * @param sysUserTable 用户列表
     * @return
     */

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult insertUser(@RequestBody SysUserTable sysUserTable) throws IOException {
        Integer code  = sysUserService.insertUser(sysUserTable);
        ResponseResult responseResult = null;
        if (code == RspStatusEnum.FAIL.getCode()) {
            responseResult=new ResponseResult(RspStatusEnum.FAIL.getCode(),"用户名称重复，请重新输入!",code.toString());
        } else {
            responseResult=new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"成功!",code.toString());
        }
        return responseResult;
    }


    /**
     * 删除用户
     *
     * @param ids 用户ID,满足批量删除
     * @return
     */

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseResult deleteUser(@RequestBody JSONObject ids)  throws IOException {
        JSONArray jsonArray = ids.getJSONArray("ids");
        List<Integer> listIds = JSONObject.parseArray(jsonArray.toJSONString(), Integer.class);
        Integer code  = sysUserService.deleteUser(listIds);
        return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),RspStatusEnum.SUCCESS.getMessage(),code.toString());
    }


    @RequestMapping(value = "/password/reset", method = RequestMethod.POST)
    public ResponseResult resetAdminUser(@RequestBody SysUser sysUser)  throws IOException {
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        SysUser currentUser = principal.getSysUser();
        if(!SysConsts.USER_TYPE_SUPER_ADMIN.equals(currentUser.getUserType())){
            throw new UnAuthorizedException(RspStatusEnum.UNAUTHORIZED);
        }
        String userType = sysUser.getUserType();
        String userName = sysUser.getUserName();
        String tenantCode = sysUser.getTenantCode();
        Boolean result = sysUserService.resetPassword(tenantCode,userType,userName);
        if(result){
            return new ResponseResult(RspStatusEnum.SUCCESS.getCode(),"密码重置成功!",RspStatusEnum.SUCCESS.getCode());
        }
        else{
            return new ResponseResult(RspStatusEnum.FAIL.getCode(),"密码重置失败!",RspStatusEnum.FAIL.getCode());
        }
    }


}
